package com.wms.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.GoodsPageQueryDTO;
import com.wms.entity.WmsGoods;
import com.wms.entity.WmsStorage;
import com.wms.exception.GoodsUpdateException;
import com.wms.mapper.GoodsMapper;
import com.wms.result.PageResult;
import com.wms.service.GoodsService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public PageResult page(GoodsPageQueryDTO goodsPageQueryDTO) {
        log.info("分页查询物品信息:{}", goodsPageQueryDTO);
        PageHelper.startPage(goodsPageQueryDTO.getPage(), goodsPageQueryDTO.getPageSize());
        Page<WmsGoods> page = goodsMapper.pageQuery(goodsPageQueryDTO);
        long total = page.getTotal();
        List<WmsGoods> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 新增物品
     * @param wmsGoods
     */
    @Override
    public void save(WmsGoods wmsGoods) {
        // 库存数量不能小于0
        if (wmsGoods.getCount() < 0){
            throw new GoodsUpdateException("库存数量不能小于0");
        }
        goodsMapper.save(wmsGoods);
    }

    /**
     * 修改物品
     * @param wmsGoods
     */
    @Override
    public void update(WmsGoods wmsGoods) {
        // 库存数量不能小于0
        if (wmsGoods.getCount() < 0){
            throw new GoodsUpdateException("库存数量不能小于0");
        }
        log.info("修改物品信息:{}", wmsGoods);
        goodsMapper.update(wmsGoods);
    }

    /**
     * 删除物品
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        goodsMapper.delete(ids);
    }

    /**
     * 根据id查询物品
     * @param id
     * @return
     */
    @Override
    public WmsGoods getById(Long id) {
        return goodsMapper.getById(id);
    }

    /**
     * 查询所有物品
     * @return
     */
    @Override
    public List<WmsGoods> getAll() {
        return goodsMapper.getAll();
    }
}
